package work.parabola.keyboardswitch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.RoleEnum;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.author.ListQuery;
import work.parabola.keyboardswitch.service.IAuthorService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/author/")
public class AuthorController {

    @Autowired
    private IAuthorService iAuthorService;

    @PostMapping ("getAuthorList.do")
    public ServerResponse getAuthorList(HttpSession session, @RequestBody ListQuery listQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iAuthorService.getAuthorList(listQuery);
    }
}
